package com.breaktian.assemble.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.os.StatFs;


import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author breaktian
 *         20150706
 *         file工具类
 */
public class FileUtil {
	/**
	 * 获得外部存储根目录
	 */
	public static String getExternalPath() {
		boolean sdCardExist = Environment.getExternalStorageState()
				.equals(Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			File sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
			return sdDir.toString();
		}
		return null;
	}

	//获得指定文件的byte数组
	public static byte[] getBytes(String filePath){
		byte[] buffer = null;
		try {
			File file = new File(filePath);
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}

	//获得指定文件的byte数组
	public static byte[] getBytes(File file){
		byte[] buffer = null;
		try {
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}

	public static void saveBitmap(String dir,String picName,Bitmap bm) {
		if(bm==null) return;
		File f = new File(dir, picName);
		if (f.exists()) {
			f.delete();
		}
		try {
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.PNG, 90, out);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}







	/**
	 * 获取缓存目录
	 * */
	public static String getCacheDir(Context context) {
		String dir = "";
		File cacheDir = null;
		if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && Environment.isExternalStorageRemovable()) {
			cacheDir = context.getCacheDir();
		} else {
			cacheDir = context.getExternalCacheDir();
		}

		if(cacheDir != null) {
			dir = cacheDir.getPath();
		}

		return dir;
	}



	public static String getFileDirPath(Context context) {
		String var1 = "";
		File var2 = context.getFilesDir();
		if(var2 != null) {
			var1 = var2.getPath();
		}

		return var1;
	}


	/**
	 * 获得外部存储的剩余空间大小
	 * 单位MB
	 */
	public static long getSDFreeSize() {
		// 取得SD卡文件路径
		File path = Environment.getExternalStorageDirectory();
		StatFs sf = new StatFs(path.getPath());
		// 获取单个数据块的大小(Byte)
		long blockSize = sf.getBlockSize();
		// 空闲的数据块的数量
		long freeBlocks = sf.getAvailableBlocks();
		// 返回SD卡空闲大小
		// return freeBlocks * blockSize; //单位Byte
		// return (freeBlocks * blockSize)/1024; //单位KB
		return (freeBlocks * blockSize) / 1024 / 1024; // 单位MB
	}

	public static long getSDAllSize() {
		// 取得SD卡文件路径
		File path = Environment.getExternalStorageDirectory();
		StatFs sf = new StatFs(path.getPath());
		// 获取单个数据块的大小(Byte)
		long blockSize = sf.getBlockSize();
		// 获取所有数据块数
		long allBlocks = sf.getBlockCount();
		// 返回SD卡大小
		// return allBlocks * blockSize; //单位Byte
		// return (allBlocks * blockSize)/1024; //单位KB
		return (allBlocks * blockSize) / 1024 / 1024; // 单位MB
	}

	/**
	 * 创建多级目录
	 */
	public static boolean createDirs(String dir) {
		File file = new File(dir);
		if (!file.exists()) {// 如果目录不存在
			file.mkdirs();// 创建多级目录
			if (file.exists()) {
				return true;
			} else {
				return false;
			}
		}
		return true;
	}


	public static File createFile(String path, String filename) {
		File file = null;
		try {
			file = new File(path, filename);
			createDirs(path);
			if (!file.exists()) {
				file.createNewFile();
				return file;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}

	public static File createFile2(String path, String filename) {
		File file = null;
		try {
			createDirs(path);
			file = new File(path, filename);
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return file;
	}

	/**
	 * 递归遍历某一目录下所有文件 包含其子目录的文件
	 * param:
	 * file-要遍历的目录
	 * listFiles 存放File的容器
	 * return:listFiles
	 */
	public static List<File> searchFiles(File file, List<File> listFiles) {
		if (!file.isFile()) {
			File[] list = file.listFiles();
			for (int i = 0; i < list.length; i++) {
				File f = list[i];
				if (f.isDirectory()) {
					searchFiles(f, listFiles);// 递归
				} else {
					listFiles.add(f);
				}
			}
		}
		return listFiles;
	}

	/**
	 * 根据路径删除指定文件
	 * param:
	 * path-文件路径
	 * return:boolean
	 */
	public static boolean delFile(String path) {
		File file = new File(path);
		if (file.exists()) {
			if (file.isFile()) {// 是文件
				file.delete();
				return true;
			}
		}
		return false;
	}

	public static boolean delFile(File file) {
		if (file.exists()) {
			if (file.isFile()) {// 是文件
				file.delete();
				return true;
			}
		}
		return false;
	}

	/**
	 * 清空某一目录
	 * param:
	 * dir-目录
	 */
	public static void clearFiles(String dir) {
		File file = new File(dir);
		if (file.isDirectory()) {
			File[] arr = file.listFiles();
			for (int i = 0; i < arr.length; i++) {
				if (arr[i].isFile()) {
					arr[i].delete();
				} else {
					// 清空子目录下的文件
					clearFiles(arr[i].getAbsolutePath());
					// 删除子目录
					arr[i].delete();
				}
			}
		}
    }


	public static void deleteDirectory(String filepath) throws IOException {
		File f = new File(filepath);

		if (f.exists() && f.isDirectory()) {
			if (f.listFiles().length == 0) {
				f.delete();
			} else {
				File delFile[] = f.listFiles();
				int i = f.listFiles().length;
				for (int j = 0; j < i; j++) {
					if (delFile[j].isDirectory()) {
						deleteDirectory(delFile[j].getAbsolutePath());
					}
					delFile[j].delete();
				}
			}
		}
	}

	public static void deleteFile(File file) {
		try {
			if (file != null && file.exists()) {
				File toFile = new File(file.getAbsolutePath() + ".tmp");
				file.renameTo(toFile);
				toFile.delete();
				toFile = null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}



}